/*
 *  Copyright 2009 Utkin Dmitry
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
 
/* 
 *  This file is part of the WSF Staff project.
 *  Please, visit http://code.google.com/p/staff for more information.
 */

// This file generated by staff_codegen
// Service Implementation

#include <rise/common/Log.h>
#include <rise/common/exmacros.h>
#include <staff/common/Exception.h>
#include <staff/security/Admin.h>
#include "AccountAdminImpl.h"

namespace staff
{
namespace admin
{

CAccountAdminImpl::CAccountAdminImpl()
{
}

CAccountAdminImpl::~CAccountAdminImpl()
{
}

::staff::admin::TUserList CAccountAdminImpl::GetUsers()
{
  ::staff::admin::TUserList tResult;
  TUser* pstUsers = NULL;
  int nUserCount = 0;

  RISE_ASSERTES(StaffSecurityAdminGetUsers(&pstUsers, &nUserCount), staff::CRemoteException, "Failed to get user list");

  for (int nIndex = 0; nIndex < nUserCount; ++nIndex)
  {
    ::staff::admin::SUser tUser;
    tUser.nId = pstUsers[nIndex].nId;
    tUser.sName = pstUsers[nIndex].szName;
    tUser.sDescription = pstUsers[nIndex].szDescription;

    tResult.push_back(tUser);
  }

  StaffSecurityAdminFreeUsers(pstUsers);

  return tResult;  // result
}

::staff::admin::TGroupList CAccountAdminImpl::GetGroups()
{
  ::staff::admin::TGroupList tResult;
  TGroup* pstGroups = NULL;
  int nGroupCount = 0;

  RISE_ASSERTES(StaffSecurityAdminGetGroups(&pstGroups, &nGroupCount), staff::CRemoteException, "Failed to get Group list");

  for (int nIndex = 0; nIndex < nGroupCount; ++nIndex)
  {
    ::staff::admin::SGroup tGroup;
    tGroup.nId = pstGroups[nIndex].nId;
    tGroup.sName = pstGroups[nIndex].szName;
    tGroup.sDescription = pstGroups[nIndex].szDescription;

    tResult.push_back(tGroup);
  }

  StaffSecurityAdminFreeGroups(pstGroups);

  return tResult;  // result
}

::staff::admin::TIdList CAccountAdminImpl::GetUserGroups(int nUserId)
{
  ::staff::admin::TIdList tResult;

  int* pnGroups = NULL;
  int nGroupCount = 0;

  RISE_ASSERTES(StaffSecurityAdminGetUserGroups(nUserId, &pnGroups, &nGroupCount), staff::CRemoteException, "Failed to get Group list");

  for (int nIndex = 0; nIndex < nGroupCount; ++nIndex)
  {
    tResult.push_back(pnGroups[nIndex]);
  }

  StaffSecurityAdminFreeGroupIds(pnGroups);

  return tResult;  // result
}

int CAccountAdminImpl::AddUser(const std::string& sUserName, const std::string& sDescription)
{
  int tResult;

  RISE_ASSERTES(StaffSecurityAdminAddUser(sUserName.c_str(), sDescription.c_str(), &tResult), staff::CRemoteException, "Can\'t add user");
  return tResult;  // result
}

void CAccountAdminImpl::RemoveUser(int nUserId)
{
  RISE_ASSERTES(StaffSecurityAdminRemoveUser(nUserId), staff::CRemoteException, "Can\'t remove user");
}

void CAccountAdminImpl::SetUserPassword(int nUserId, const std::string& sPass)
{
  RISE_ASSERTES(StaffSecurityAdminSetUserPassword(nUserId, sPass.c_str()), staff::CRemoteException, "Can\'t set user password");
}

int CAccountAdminImpl::AddGroup(const std::string& sGroupName, const std::string& sDescription)
{
  int tResult;

  RISE_ASSERTES(StaffSecurityAdminAddGroup(sGroupName.c_str(), sDescription.c_str(), &tResult), staff::CRemoteException, "Can\'t add group");
  return tResult;  // result
}

void CAccountAdminImpl::RemoveGroup(int nGroupId)
{
  RISE_ASSERTES(StaffSecurityAdminRemoveGroup(nGroupId), staff::CRemoteException, "Can\'t remove group");
}

void CAccountAdminImpl::AddUserToGroup(int nUserId, int nGroupId)
{
  RISE_ASSERTES(StaffSecurityAdminAddUserToGroup(nUserId, nGroupId), staff::CRemoteException, "Can\'t include user into group");
}

void CAccountAdminImpl::AddUserToGroups(int nUserId, const ::staff::admin::TIdList& rlsGroupIds)
{
  int* pnGroups = (int*)calloc(rlsGroupIds.size(), sizeof(int));
  int* pnFailedGroups = NULL;

  RISE_ASSERTP(pnGroups);
  try
  {
    int i = 0;
    for(::staff::admin::TIdList::const_iterator itGroup = rlsGroupIds.begin(); itGroup != rlsGroupIds.end(); ++itGroup, ++i)
    {
      pnGroups[i] = *itGroup;
    }
    
    RISE_ASSERTES(StaffSecurityAdminAddUserToGroups(nUserId, pnGroups, rlsGroupIds.size(), pnFailedGroups), staff::CRemoteException, "Can\'t include user into groups");
  }
  catch(...)
  {
    free(pnGroups);
    throw;
  }

  free(pnGroups);
}

void CAccountAdminImpl::RemoveUserFromGroup(int nUserId, int nGroupId)
{
  RISE_ASSERTES(StaffSecurityAdminRemoveUserFromGroup(nUserId, nGroupId), staff::CRemoteException, "Can\'t exclude user from group");
}

void CAccountAdminImpl::RemoveUserFromGroups(int nUserId, const ::staff::admin::TIdList& rlsGroupIds)
{
  int* pnGroups = (int*)calloc(rlsGroupIds.size(), sizeof(int));
  int* pnFailedGroups = NULL;

  RISE_ASSERTP(pnGroups);
  try
  {
    int i = 0;
    for(::staff::admin::TIdList::const_iterator itGroup = rlsGroupIds.begin(); itGroup != rlsGroupIds.end(); ++itGroup, ++i)
    {
      pnGroups[i] = *itGroup;
    }
    
    RISE_ASSERTES(StaffSecurityAdminRemoveUserFromGroups(nUserId, pnGroups, rlsGroupIds.size(), pnFailedGroups), staff::CRemoteException, "Can\'t exclude user from groups");
  }
  catch(...)
  {
    free(pnGroups);
    throw;
  }

  free(pnGroups);
}


}
}

